import { lookup } from "./utils";

export default function renderTemplate(tokens, data) {
    let str = '';

    for (let i = 0; i < tokens.length; i++) {
        const token = tokens[i];

        if (token[0] === 'name') {
            str += lookup(data, token[1]);
        } else if(token[0] === 'text'){
            str += token[1];
        } else if (token[0] === '#') {
            const values = lookup(data, token[1]);
            for (let j = 0; j < values.length; j++) {
                str += renderTemplate(token[2], {
                    ...values[j],
                    ".": values[j]
                });
            }
        }
    }

    return str;
};